package com.bfsuol.bfsuolframework.core.spring;

import com.bfsuol.bfsuolframework.core.controller.support.DefaultQueryParam;
import com.bfsuol.bfsuolframework.core.controller.support.QueryParam;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class QueryParameterMethodArgumentResolver implements HandlerMethodArgumentResolver {

	public QueryParameterMethodArgumentResolver() {	}

	public boolean supportsParameter(MethodParameter parameter) {
		if(QueryParam.class.isAssignableFrom(parameter.getParameterType())){
			return true;
		}
		return false;
	}

	public Object resolveArgument(MethodParameter parameter,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
			WebDataBinderFactory binderFactory) throws Exception {
		
		QueryParam queryParam = new DefaultQueryParam();
		String query_name = getQueryName(parameter);
		String query_prefix = query_name.endsWith(".") ? query_name : query_name+".";
				
        for(String key : webRequest.getParameterMap().keySet()){
        	if(key.startsWith(query_prefix) && key.length() > query_prefix.length()){
        		handlerParameter(key.substring(query_prefix.length()),webRequest.getParameterValues(key),queryParam);
        	}
        }

        if (binderFactory != null) {
			WebDataBinder binder = binderFactory.createBinder(webRequest, null, query_prefix);
			queryParam = binder.convertIfNecessary(queryParam, QueryParam.class, parameter);
		}
        
        mavContainer.addAttribute(query_name, queryParam);
        
		return queryParam;
		
		
	}
	
	private void handlerParameter(String name,String[] values,QueryParam queryParam){
		if(name == null || values == null || values.length == 0){
			return;
		}
		
		if(name.equalsIgnoreCase("order")){
			for(String value : values){
				if(StringUtils.hasText(value)){
					queryParam.addOrder(value);
				}
			}
		}else{
			if(values.length == 1){
				if(StringUtils.hasText(values[0])){
					queryParam.addQuery(name, values[0], true);
				}
			}else{
				int index = 0;
				String query;
				for(String value : values){
					if(StringUtils.hasText(value)){
						if(name.indexOf("@")>-1){
							query = name + index;
						}else{
							query = name + "@s"+index;
						}
						queryParam.addQuery(query, value, true);
						index++;
					}
				}
			}
		}
	}
	
	/**
	 * 得到查询参数的前缀
	 * @param parameter
	 * @return
	 */
	private String getQueryName(MethodParameter parameter){
		String prefix = null;
		QueryParameter annotation = parameter.getParameterAnnotation(QueryParameter.class);
		if(annotation != null && annotation.value() != null){
			prefix = annotation.value();
		}else{
			prefix = QueryParameter.DEFAULT_QUERY_NAME;
		}
		return prefix;
	}
}
